package com.cn.kabalifeuser.ui.my.activity;

import com.cn.kabalifeuser.api.Api;
import com.cn.kabalifeuser.base.BasePresenter;
import com.cn.kabalifeuser.callback.CacheCallBack;
import com.cn.kabalifeuser.config.Constants;
import com.cn.kabalifeuser.config.RequestParamConfig;
import com.cn.kabalifeuser.ui.login.bean.LoginBean;
import com.cn.kabalifeuser.ui.login.bean.MsgBean;
import com.cn.kabalifeuser.ui.my.bean.AppVersionBean;
import com.cn.kabalifeuser.ui.my.bean.MyUserInfoBean;
import com.cn.kabalifeuser.ui.my.bean.UpImgBean;
import com.cn.kabalifeuser.utils.LoggerUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.PostRequest;

import java.io.File;

/**
 * Created by 晓林 on 2020/9/8.
 * function:
 */
public class UserInfoPresenter extends BasePresenter<UserInfoView> {
    public void onUserInfoData(String token) {
        OkGo.<MyUserInfoBean>get(Api.API_USER_INFO)
                .headers("token", token)
                .execute(new CacheCallBack<MyUserInfoBean>() {
                    @Override
                    public void onSuccess(Response<MyUserInfoBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("我的" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onUserInfoSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

    /**
     * 上传文件。
     */
    public void onUpImgData(String type, String img) {
        PostRequest postRequest = OkGo.<UpImgBean>post(Api.API_UP_IMG);
        postRequest.params("type", type);
        postRequest.params("file[]", new File(img));
        postRequest.execute(new CacheCallBack<UpImgBean>(getView()._getContext(), true) {
            @Override
            public void onSuccess(Response<UpImgBean> response) {
                super.onSuccess(response);
                LoggerUtils.d("上传图:" + response.body());
                if (isViewAttached())
                    if (response.body().getCode() == Constants.HTTP_SUCCESS) {
                        getView().onUpImgSuccess(response.body());
                    } else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN) {
                        getView().onReLoggedIn(response.body().getMsg());
                    } else {
                        getView().onError(response.body().getMsg());
                    }

            }
        });
    }

    /**
     * 头像
     *
     * @param token
     * @param head
     */
    public void onUpUserInfoData(String token, String head) {
        OkGo.<MsgBean>post(Api.API_USER_INFO_UP)
                .headers("token", token)
                .params("head", head)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("修改个人信息：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onUpUserInfoSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }

    /**
     * 昵称
     *
     * @param token
     * @param nickname
     */
    public void onUpUserInfoNameData(String token, String nickname) {
        OkGo.<MsgBean>post(Api.API_USER_INFO_UP)
                .headers("token", token)
                .params("nickname", nickname)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("修改个人信息：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onUpUserInfoSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }

    /**
     * 性别
     *
     * @param token
     * @param sex
     */
    public void onUpUserInfoSexData(String token, String sex) {
        OkGo.<MsgBean>post(Api.API_USER_INFO_UP)
                .headers("token", token)
                .params("sex", sex)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("修改个人信息：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onUpUserInfoSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }


    /**
     * 退出
     *
     * @param token
     */
    public void onUserOutData(String token) {
        OkGo.<MsgBean>post(Api.API_USER_OUT)
                .headers("token", token)
                .execute(new CacheCallBack<MsgBean>() {
                    @Override
                    public void onSuccess(Response<MsgBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("修改个人信息：" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onUserOutSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());


                    }
                });
    }


    public void onAppVersionData(String token) {
        OkGo.<AppVersionBean>get(Api.API_VERSION)
                .headers("token", token)
                .params("name", 1)
                .params("type", 1)
                .execute(new CacheCallBack<AppVersionBean>() {
                    @Override
                    public void onSuccess(Response<AppVersionBean> response) {
                        super.onSuccess(response);
                        LoggerUtils.e("我的" + response.body());
                        if (isViewAttached())
                            if (response.body().getCode() == Constants.HTTP_SUCCESS)
                                getView().onAppVersionSuccess(response.body());
                            else if (response.body().getCode() == Constants.HTTP_AGAIN_LOGIN)
                                getView().onReLoggedIn(response.body().getMsg());
                            else
                                getView().onError(response.body().getMsg());
                    }
                });
    }

}
